<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Linux," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;Linux系统拥有非常灵活和强大的日志功能，可以保存几乎所有的操作记录，并可以从中检索出我们需要的信息。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;大部分Linux发行版默认的日志守护进程为 syslog，位于 /etc/syslog 或 /etc/">
<meta name="keywords" content="Linux">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux系统日志及日志分析">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/1. Linux 基础/67. Linux系统日志及日志分析/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;Linux系统拥有非常灵活和强大的日志功能，可以保存几乎所有的操作记录，并可以从中检索出我们需要的信息。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;大部分Linux发行版默认的日志守护进程为 syslog，位于 /etc/syslog 或 /etc/">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-09-01T08:31:48.348Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Linux系统日志及日志分析">
<meta name="twitter:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;Linux系统拥有非常灵活和强大的日志功能，可以保存几乎所有的操作记录，并可以从中检索出我们需要的信息。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;大部分Linux发行版默认的日志守护进程为 syslog，位于 /etc/syslog 或 /etc/">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/1. Linux 基础/67. Linux系统日志及日志分析/"/>





  <title>Linux系统日志及日志分析 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/1. Linux 基础/67. Linux系统日志及日志分析/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Linux系统日志及日志分析</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T01:55:11+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Linux系统拥有非常灵活和强大的日志功能，可以保存几乎所有的操作记录，并可以从中检索出我们需要的信息。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;大部分Linux发行版默认的日志守护进程为 syslog，位于 /etc/syslog 或 /etc/syslogd，默认配置文件为 /etc/syslog.conf，任何希望生成日志的程序都可以向 syslog 发送信息。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Linux系统内核和许多程序会产生各种错误信息、警告信息和其他的提示信息，这些信息对管理员了解系统的运行状态是非常有用的，所以应该把它们写到日志文件中去。完成这个过程的程序就是syslog。syslog可以根据日志的类别和优先级将日志保存到不同的文件中。例如，为了方便查阅，可以把内核信息与其他信息分开，单独保存到一个独立的日志文件中。默认配置下，日志文件通常都保存在“/var/log”目录下。</p>
<h2 id="日志类型"><a href="#日志类型" class="headerlink" title="日志类型"></a>日志类型</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面是常见的日志类型，但并不是所有的Linux发行版都包含这些类型：</p>
<table>
<thead>
<tr>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>auth</td>
<td>用户认证时产生的日志，如login命令、su命令。</td>
</tr>
<tr>
<td>authpriv</td>
<td>与 auth 类似，但是只能被特定用户查看。</td>
</tr>
<tr>
<td>console</td>
<td>针对系统控制台的消息。</td>
</tr>
<tr>
<td>cron</td>
<td>系统定期执行计划任务时产生的日志。</td>
</tr>
<tr>
<td>daemon</td>
<td>某些守护进程产生的日志。</td>
</tr>
<tr>
<td>ftp</td>
<td>FTP服务。</td>
</tr>
<tr>
<td>kern</td>
<td>系统内核消息。</td>
</tr>
<tr>
<td>local0.local7</td>
<td>由自定义程序使用。</td>
</tr>
<tr>
<td>lpr</td>
<td>与打印机活动有关。</td>
</tr>
<tr>
<td>mail</td>
<td>邮件日志。</td>
</tr>
<tr>
<td>mark</td>
<td>产生时间戳。系统每隔一段时间向日志文件中输出当前时间，每行的格式类似于 May 26 11:17:09 rs2 – MARK –，可以由此推断系统发生故障的大概时间。</td>
</tr>
<tr>
<td>news</td>
<td>网络新闻传输协议(nntp)产生的消息。</td>
</tr>
<tr>
<td>ntp</td>
<td>网络时间协议(ntp)产生的消息。</td>
</tr>
<tr>
<td>user</td>
<td>用户进程。uucpUUCP子系统。</td>
</tr>
</tbody>
</table>
<h2 id="日志优先级"><a href="#日志优先级" class="headerlink" title="日志优先级"></a>日志优先级</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;常见的日志优先级请见下标：</p>
<table>
<thead>
<tr>
<th>优先级</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>emerg</td>
<td>紧急情况，系统不可用（例如系统崩溃），一般会通知所有用户。</td>
</tr>
<tr>
<td>alert</td>
<td>需要立即修复，例如系统数据库损坏。</td>
</tr>
<tr>
<td>crit</td>
<td>危险情况，例如硬盘错误，可能会阻碍程序的部分功能。</td>
</tr>
<tr>
<td>err</td>
<td>一般错误消息。</td>
</tr>
<tr>
<td>warning</td>
<td>警告。</td>
</tr>
<tr>
<td>notice</td>
<td>不是错误，但是可能需要处理。</td>
</tr>
<tr>
<td>info</td>
<td>通用性消息，一般用来提供有用信息。</td>
</tr>
<tr>
<td>debug</td>
<td>调试程序产生的信息。</td>
</tr>
<tr>
<td>none</td>
<td>没有优先级，不记录任何日志消息。</td>
</tr>
</tbody>
</table>
<h2 id="常见日志文件"><a href="#常见日志文件" class="headerlink" title="常见日志文件"></a>常见日志文件</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;所有的系统应用都会在 /var/log 目录下创建日志文件，或创建子目录再创建日志文件。例如：</p>
<table>
<thead>
<tr>
<th>文件/目录</th>
<th>说明/</th>
</tr>
</thead>
<tbody>
<tr>
<td>var/log/boot.log</td>
<td>开启或重启日志。</td>
</tr>
<tr>
<td>/var/log/cron</td>
<td>计划任务日志</td>
</tr>
<tr>
<td>/var/log/maillog</td>
<td>邮件日志。</td>
</tr>
<tr>
<td>/var/log/messages</td>
<td>该日志文件是许多进程日志文件的汇总，从该文件可以看出任何入侵企图或成功的入侵。</td>
</tr>
<tr>
<td>/var/log/httpd 目录</td>
<td>Apache HTTP 服务日志。</td>
</tr>
<tr>
<td>/var/log/samba 目录</td>
<td>samba 软件日志</td>
</tr>
</tbody>
</table>
<h2 id="etc-syslog-conf-文件"><a href="#etc-syslog-conf-文件" class="headerlink" title="/etc/syslog.conf 文件"></a>/etc/syslog.conf 文件</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/etc/syslog.conf 是 syslog 的配置文件，会根据日志类型和优先级来决定将日志保存到何处。典型的 syslog.conf 文件格式如下所示：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">*.err;kern.debug;auth.notice /dev/console</div><div class="line">daemon,auth.notice           /var/<span class="built_in">log</span>/messages</div><div class="line">lpr.info                     /var/<span class="built_in">log</span>/lpr.log</div><div class="line">mail.*                       /var/<span class="built_in">log</span>/mail.log</div><div class="line">ftp.*                        /var/<span class="built_in">log</span>/ftp.log</div><div class="line">auth.*                       @see.xidian.edu.cn</div><div class="line">auth.*                       root,amrood</div><div class="line">netinfo.err                  /var/<span class="built_in">log</span>/netinfo.log</div><div class="line">install.*                    /var/<span class="built_in">log</span>/install.log</div><div class="line">*.emerg                      *</div><div class="line">*.alert                      |program_name</div><div class="line">mark.*                       /dev/console</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第一列为日志类型和日志优先级的组合，每个类型和优先级的组合称为一个选择器；后面一列为保存日志的文件、服务器，或输出日志的终端。syslog 进程根据选择器决定如何操作日志。</p>
<h3 id="对配置文件的几点说明："><a href="#对配置文件的几点说明：" class="headerlink" title="对配置文件的几点说明："></a>对配置文件的几点说明：</h3><ul>
<li>日志类型和优先级由点号(.)分开，例如 kern.debug 表示由内核产生的调试信息。</li>
<li>kern.debug 的优先级大于 debug。</li>
<li>星号(*)表示所有，例如 *.debug 表示所有类型的调试信息，kern.* 表示由内核产生的所有消息。</li>
<li>可以使用逗号(,)分隔多个日志类型，使用分号(;)分隔多个选择器。</li>
</ul>
<h3 id="对日志的操作包括："><a href="#对日志的操作包括：" class="headerlink" title="对日志的操作包括："></a>对日志的操作包括：</h3><ul>
<li>将日志输出到文件，例如 /var/log/maillog 或 /dev/console。</li>
<li>将消息发送给用户，多个用户用逗号(,)分隔，例如 root, amrood。</li>
<li>通过管道将消息发送给用户程序，注意程序要放在管道符(|)后面。</li>
<li>将消息发送给其他主机上的 syslog 进程，这时 /etc/syslog.conf 文件后面一列为以@开头的主机名，例如@see.xidian.edu.cn。</li>
</ul>
<h2 id="logger-命令"><a href="#logger-命令" class="headerlink" title="logger 命令"></a>logger 命令</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;logger 是Shell命令，可以通过该命令使用 syslog 的系统日志模块，还可以从命令行直接向系统日志文件写入一行信息。</p>
<h3 id="logger命令的语法为："><a href="#logger命令的语法为：" class="headerlink" title="logger命令的语法为："></a>logger命令的语法为：</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">logger [-i] [-f filename] [-p priority] [-t tag] [message...]</div></pre></td></tr></table></figure>
<p>每个选项的含义如下：</p>
<table>
<thead>
<tr>
<th>选项</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>-f filename</td>
<td>将 filename 文件的内容作为日志。</td>
</tr>
<tr>
<td>-i</td>
<td>每行都记录 logger 进程的ID。</td>
</tr>
<tr>
<td>-p priority</td>
<td>指定优先级；优先级必须是形如 facility.priority 的完整的选择器，默认优先级为 user.notice。</td>
</tr>
<tr>
<td>-t tag</td>
<td>使用指定的标签标记每一个记录行。</td>
</tr>
<tr>
<td>message</td>
<td>要写入的日志内容，多条日志以空格为分隔；如果没有指定日志内容，并且 -f filename 选项为空，那么会把标准输入作为日志内容。</td>
</tr>
</tbody>
</table>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如，将ping命令的结果写入日志：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">$ ping 192.168.0.1 | logger -it logger_test -p local3.notice&amp;</div><div class="line">$ tail -f /var/<span class="built_in">log</span>/userlog</div><div class="line">Oct 6 12:48:43 kevein logger_test[22484]: PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.</div><div class="line">Oct 6 12:48:43 kevein logger_test[22484]: 64 bytes from 192.168.0.1: icmp_seq=1 ttl=253 time=49.7 ms</div><div class="line">Oct 6 12:48:44 kevein logger_test[22484]: 64 bytes from 192.168.0.1: icmp_seq=2 ttl=253 time=68.4 ms</div><div class="line">Oct 6 12:48:45 kevein logger_test[22484]: 64 bytes from 192.168.0.1: icmp_seq=3 ttl=253 time=315 ms</div><div class="line">Oct 6 12:48:46 kevein logger_test[22484]: 64 bytes from 192.168.0.1: icmp_seq=4 ttl=253 time=279 ms</div><div class="line">Oct 6 12:48:47 kevein logger_test[22484]: 64 bytes from 192.168.0.1: icmp_seq=5 ttl=253 time=347 ms</div><div class="line">Oct 6 12:48:49 kevein logger_test[22484]: 64 bytes from 192.168.0.1: icmp_seq=6 ttl=253 time=701 ms</div><div class="line">Oct 6 12:48:50 kevein logger_test[22484]: 64 bytes from 192.168.0.1: icmp_seq=7 ttl=253 time=591 ms</div><div class="line">Oct 6 12:48:51 kevein logger_test[22484]: 64 bytes from 192.168.0.1: icmp_seq=8 ttl=253 time=592 ms</div><div class="line">Oct 6 12:48:52 kevein logger_test[22484]: 64 bytes from 192.168.0.1: icmp_seq=9 ttl=253 time=611 ms</div><div class="line">Oct 6 12:48:53 kevein logger_test[22484]: 64 bytes from 192.168.0.1: icmp_seq=10 ttl=253 time=931 ms</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ping命令的结果成功输出到 /var/log/userlog 文件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;命令 logger -it logger_test -p local3.notice 各选项的含义：</p>
<ul>
<li>-i：在每行都记录进程ID；</li>
<li>-t logger_test：每行记录都加上“logger_test”这个标签；</li>
<li>-p local3.notice：设置日志类型和优先级。</li>
</ul>
<h2 id="日志转储"><a href="#日志转储" class="headerlink" title="日志转储"></a>日志转储</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;日志转储也叫日志回卷或日志轮转。Linux中的日志通常增长很快，会占用大量硬盘空间，需要在日志文件达到指定大小时分开存储。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;syslog 只负责接收日志并保存到相应的文件，但不会对日志文件进行管理，因此经常会造成日志文件过大，尤其是WEB服务器，轻易就能超过1G，给检索带来困难。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;大多数Linux发行版使用 logrotate 或 newsyslog 对日志进行管理。logrotate 程序不但可以压缩日志文件，减少存储空间，还可以将日志发送到指定 E-mail，方便管理员及时查看日志。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如，规定邮件日志 /var/log/maillog 超过1G时转储，每周一次，那么每隔一周 logrotate 进程就会检查 /var/log/maillog 文件的大小：</p>
<ul>
<li>如果没有超过1G，不进行任何操作。</li>
<li>如果在1G~2G之间，就会创建新文件 /var/log/maillog.1，并将多出的1G日志转移到该文件，以给 /var/log/maillog 文件瘦身。</li>
<li>如果在2G~3G之间，会继续创建新文件 /var/log/maillog.2，并将 /var/log/maillog.1 的内容转移到该文件，将 /var/log/maillog 的内容转移到 /var/log/maillog.1，以保持 /var/log/maillog 文件不超过1G。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以看到，每次转存都会创建一个新文件（如果不存在），命名格式为日志文件名加一个数字（从1开始自动增长），以保持当前日志文件和转存后的日志文件不超过指定大小。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;logrotate 的主要配置文件是 /etc/logrotate.conf，/etc/logrotate.d 目录是对 /etc/logrotate.conf 的补充，或者说为了不使 /etc/logrotate.conf 过大而设置。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以通过 cat 命令查看它的内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$cat</span> /etc/logrotate.conf</div><div class="line"><span class="comment"># see "man logrotate" for details //可以查看帮助文档</span></div><div class="line"><span class="comment"># rotate log files weekly</span></div><div class="line">weekly //设置每周转储一次</div><div class="line"><span class="comment"># keep 4 weeks worth of backlogs</span></div><div class="line">rotate 4 //最多转储4次</div><div class="line"><span class="comment"># create new (empty) log files after rotating old ones</span></div><div class="line">create //当转储后文件不存储时创建它</div><div class="line"><span class="comment"># uncomment this if you want your log files compressed</span></div><div class="line"><span class="comment">#compress //以压缩方式转储</span></div><div class="line"><span class="comment"># RPM packages drop log rotation information into this directory</span></div><div class="line">include /etc/logrotate.d //其他日志文件的转储方式，包含在该目录下</div><div class="line"><span class="comment"># no packages own wtmp -- we'll rotate them here</span></div><div class="line">/var/<span class="built_in">log</span>/wtmp &#123; //设置/var/<span class="built_in">log</span>/wtmp日志文件的转储参数</div><div class="line"> monthly //每月转储</div><div class="line"> create 0664 root utmp //转储后文件不存在时创建它，文件所有者为root，所属组为utmp，对应的权限为0664</div><div class="line"> rotate 1 //最多转储一次</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：include 允许管理员把多个分散的文件集中到一个，类似于C语言的 #include，将其他文件的内容包含进当前文件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;include 非常有用，一些程序会把转储日志的配置文件放在 /etc/logrotate.d 目录，这些配置文件会覆盖或增加 /etc/logrotate.conf 的配置项，如果没有指定相关配置，那么采用 /etc/logrotate.conf 的默认配置。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;所以，建议将 /etc/logrotate.conf 作为默认配置文件，第三方程序在 /etc/logrotate.d 目录下自定义配置文件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;logrotate 也可以作为命令直接运行来修改配置文件。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Linux/" rel="tag"># Linux</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/1. Linux 基础/52. 详细说明Buffer和Cache的区别/" rel="next" title="详细说明Buffer和Cache的区别">
                <i class="fa fa-chevron-left"></i> 详细说明Buffer和Cache的区别
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/1. Linux 基础/33. vim给文件加密/" rel="prev" title="vim给文件加密">
                vim给文件加密 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#日志类型"><span class="nav-number">1.</span> <span class="nav-text">日志类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#日志优先级"><span class="nav-number">2.</span> <span class="nav-text">日志优先级</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#常见日志文件"><span class="nav-number">3.</span> <span class="nav-text">常见日志文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#etc-syslog-conf-文件"><span class="nav-number">4.</span> <span class="nav-text">/etc/syslog.conf 文件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#对配置文件的几点说明："><span class="nav-number">4.1.</span> <span class="nav-text">对配置文件的几点说明：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#对日志的操作包括："><span class="nav-number">4.2.</span> <span class="nav-text">对日志的操作包括：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#logger-命令"><span class="nav-number">5.</span> <span class="nav-text">logger 命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#logger命令的语法为："><span class="nav-number">5.1.</span> <span class="nav-text">logger命令的语法为：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#日志转储"><span class="nav-number">6.</span> <span class="nav-text">日志转储</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
